<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var majorityElement = function (nums) {
      let maxCount = 0;
      let ans = nums[0];
      let map = {};

      for (let i = 0; i < nums.length; i++) {
        if (map[nums[i]]) map[nums[i]]++;
        else map[nums[i]] = 1;

        if (maxCount < map[nums[i]]) {
          maxCount = map[nums[i]];
          ans = nums[i];
        }
      }
      return ans;
    };
    console.log(majorityElement(nums = [3, 2, 3]))
  </script>
</body>

</html>